package com.zhang.authoritycenter.common.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;

/**
 * 深圳金雅福控股集团有限公司
 *
 * @author zhangfanjun
 * @date 2022/4/22
 */
public class AESUtil0 {
    private static final String CHARSET = "utf-8";
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public AESUtil0() {
    }

    public static String encryptForHex(String content, String keyStr) {
        if (StringUtils.isBlank(content)) {
            return null;
        } else {
            try {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                byte[] byteContent = content.getBytes("utf-8");
                SecretKeySpec kp = new SecretKeySpec(keyStr.getBytes("utf-8"), "AES");
                cipher.init(1, kp);
                byte[] result = cipher.doFinal(byteContent);
                return Hex.encodeHexString(result);
            } catch (Exception var6) {
                throw new RuntimeException(var6);
            }
        }
    }

    public static String decryptForHex(String content, String keyStr) {
        if (StringUtils.isBlank(content)) {
            return null;
        } else {
            try {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                SecretKeySpec kp = new SecretKeySpec(keyStr.getBytes("utf-8"), "AES");
                cipher.init(2, kp);
                byte[] result = cipher.doFinal(Hex.decodeHex(content.toCharArray()));
                return new String(result, "utf-8");
            } catch (Exception var5) {
                throw new RuntimeException(var5);
            }
        }
    }

    public static String encrypt(String content, String keyStr) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] byteContent = content.getBytes("utf-8");
            SecretKeySpec kp = new SecretKeySpec(keyStr.getBytes("utf-8"), "AES");
            cipher.init(1, kp);
            byte[] result = cipher.doFinal(byteContent);
            return Base64.encodeBase64String(result);
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    public static String decrypt(String content, String keyStr) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec kp = new SecretKeySpec(keyStr.getBytes("utf-8"), "AES");
            cipher.init(2, kp);
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, "utf-8");
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    public static String decryptDataWX(String content, String keyStr) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            String keyMd5String = DigestUtils.md5Hex(keyStr).toLowerCase();
            SecretKeySpec kp = new SecretKeySpec(keyMd5String.getBytes("utf-8"), "AES");
            cipher.init(2, kp);
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, "utf-8");
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    private static AlgorithmParameterSpec getIV() {
        byte[] iv = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        return ivParameterSpec;
    }

    public static String decrypts(String content, String keyStr) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            String keyMd5String = DigestUtils.md5Hex(keyStr).toLowerCase();
            SecretKeySpec kp = new SecretKeySpec(keyMd5String.getBytes("utf-8"), "ECB");
            cipher.init(2, kp);
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, "utf-8");
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static void main(String[] args) {
        A1();
        A2();
    }

    static void A1() {
        String dds = encryptForHex("123456_1580914127295", "35e66ef603f1d503cbab4162");
        System.out.println(dds);
        String dd = decryptForHex("5615c251f8907a80f37f9297a380ef1ef0864fc9c461622e831b0103d9cad6c4", "35e66ef603f1d503cbab4162");
        System.out.println(dd.contains("_") ? dd.split("_")[0] : dd);
    }

    static void A2() {
        String s = "欢迎来到github.net";
        System.out.println("s:" + s);
        String s1 = encryptForHex(s, "1234567890123456");
        System.out.println("s1:" + s1);
        System.out.println("s2:" + decryptForHex(s1, "1234567890123456"));
    }
}

